# ExperienceOrb.gd
class_name ExperienceOrb
extends Area2D

@export var experience_value: int = 1
@export var move_speed: float = 150.0 # Speed towards player when in range

var target_player: Node2D = null

# Collision: Layer 6 (pickups), Mask 2 (player)

func _ready():
	# Ensure the Area2D monitors for bodies entering its shape
	# Connect body_entered in the editor or here
	body_entered.connect(_on_body_entered)
	# Find player's pickup area reference? Or just check distance?
	# Using player's pickup area is often better performance-wise


func _physics_process(delta):
	if target_player:
		# Move towards the player
		global_position = global_position.move_toward(target_player.global_position, move_speed * delta)


func _on_body_entered(body):
	# Check if the body that entered is the player
	if body is Player:
		print("Orb collected by player")
		body.collect_orb(experience_value)
		queue_free() # Destroy the orb


# Alternative: Triggered by Player's PickupArea signal
func on_player_pickup_area_entered(area):
	# If the entering area is this orb
	if area == self:
		target_player = get_tree().get_first_node_in_group("player") # Find player
		# Disable collision shape so it doesn't trigger multiple times? Or rely on queue_free
		# $CollisionShape2D.disabled = true
